package com.qf.ithub.movie_demo01.controller;

import com.qf.ithub.common.result.R;
import com.qf.ithub.common.result.ResponseEnum;
import com.qf.ithub.movie_demo01.common.aop.CheckRight;
import com.qf.ithub.movie_demo01.entity.dto.GrantDTO;
import com.qf.ithub.movie_demo01.entity.dto.MenuDTO;
import com.qf.ithub.movie_demo01.service.impl.RrightServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : xixi
 * @Title :
 * @date :  2021-03-19 01:16
 */
@RestController
@RequestMapping("/admin/right")
@Api(description = "管理员使用的权限接口")
@CrossOrigin("*")
@Slf4j
public class RightController {

    @Autowired
    private RrightServiceImpl rrightService;

    /*
     * 测试查询接口
     * */
    @GetMapping("/test/search/check")
    @CheckRight("TEST_SEARCH")
    public R testSearch() {
        return new R(ResponseEnum.SUCCESS, null);
    }

    /*
     * 测试审批接口
     * */
    @GetMapping("/test/audit/check")
    @CheckRight("TEST_AUDIT")
    public R testAudit() {
        return new R(ResponseEnum.SUCCESS, null);
    }


    /**
     * 获得系统中所有的菜单权限 层级格式返回
     */
    @ApiOperation("获得系统中所有的权限 层级格式返回")
    @GetMapping("/rights")
    public R getRightsByRoleid() {
        // System.out.println(roleid);
        List<MenuDTO> menuDTOS = rrightService.selectRightsByParentid();
        return new R(ResponseEnum.SUCCESS, menuDTOS);
    }

    /**
     * 根据角色id 查询出 这个角色的拥有的权限id的集合 不包含父节点
     */
    @ApiOperation("根据角色id 查询出 这个角色的拥有的权限id的集合 不包含父节点")
    @GetMapping("/rights/grant/{roleid}")
    public R getRightsByRoleidNoParent(
            @ApiParam(value = "角色id", required = true) @PathVariable("roleid") Integer roleid) {
        List<Integer> integers = rrightService.selectRightidsByRoleNoParent(roleid);
        return new R(ResponseEnum.SUCCESS, integers);
    }

    /**
     * 给指定的角色赋予菜单权限
     */
    @PostMapping("/rights/grant/menus")
    public R grantMenuRights(@RequestBody GrantDTO dto) {
        System.out.println(dto);
        rrightService.grantMenu(dto);
        return new R(ResponseEnum.SUCCESS, null);
    }
}
